package com.yinhai.message.manage.register.bpo;

import java.util.List;
import java.util.Properties;

import javax.mail.Message;

import org.simplejavamail.MailException;
import org.simplejavamail.email.Email;
import org.simplejavamail.mailer.Mailer;
import org.simplejavamail.mailer.config.TransportStrategy;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.util.StringUtil;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.core.service.ta3.domain.service.TaBaseService;
import com.yinhai.message.manage.register.dao.IEmailConfigDao;
import com.yinhai.message.manage.register.po.EmailConfigPo;
import com.yinhai.message.manage.register.vo.EmailConfigVo;
import com.yinhai.message.manage.sysapp.bpo.ISysAppBpo;
import com.yinhai.message.manage.sysapp.vo.SysAppVo;

/**
 * 邮箱配置业务处理
 * @author create by lanyc on 2017/1/4
 *
 */
public class EmailConfigBpoImpl extends TaBaseService implements IEmailConfigBpo {
	/**
	 * 邮箱配置DAO接口
	 */
	private IEmailConfigDao emailConfigDao;
	/**
	 * 接入SysAppBpo，获取接入系统信息
	 */
	private ISysAppBpo sysAppBpo;
	
	public ISysAppBpo getSysAppBpo() {
		return sysAppBpo;
	}

	public void setSysAppBpo(ISysAppBpo sysAppBpo) {
		this.sysAppBpo = sysAppBpo;
	}

	public IEmailConfigDao getEmailConfigDao() {
		return emailConfigDao;
	}

	public void setEmailConfigDao(IEmailConfigDao emailConfigDao) {
		this.emailConfigDao = emailConfigDao;
	}

	@Override
	public List<EmailConfigVo> queryAllEmailConfig() {
		
		return emailConfigDao.queryAllEmailConfig();
	}

	@Override
	public Integer addEmailConfig(EmailConfigPo emailConfigPo) {
		validateEmailAddress(emailConfigPo);
		//如果设置为默认，清除原来的默认邮箱
        if(emailConfigPo.getDefaultemail()==1){
        	emailConfigDao.removeDefault();
        }
        try{
			emailConfigDao.addEmailConfig(emailConfigPo);
			return 1;
		}catch(Exception e){
			return -1;
		}
	}
	
	/**
	 * 验证邮箱是否真实有效
	 * @param emailConfigPo
	 */
	public Integer validateEmailAddress(EmailConfigPo emailConfigPo) throws MailException{
		//验证邮箱是否有效
		Email email = new Email();
		email.setFromAddress("",emailConfigPo.getEmailAddress());
		email.setSubject("测试连接!");
		email.addRecipient("",emailConfigPo.getEmailAddress(),Message.RecipientType.TO);
		email.setText("测试");
		
        Mailer mailer = null;
		if(emailConfigPo.getIsSendSSL()==1){
			mailer = new Mailer(emailConfigPo.getSendHost(),emailConfigPo.getSendPort(),emailConfigPo.getEmailAddress(),emailConfigPo.getPassword(),TransportStrategy.SMTP_SSL);
        }else if(emailConfigPo.getIsSendTLS()==1){
        	mailer = new Mailer(emailConfigPo.getSendHost(),emailConfigPo.getSendPort(),emailConfigPo.getEmailAddress(),emailConfigPo.getPassword(),TransportStrategy.SMTP_TLS);
        }else{
        	mailer = new Mailer(emailConfigPo.getSendHost(),emailConfigPo.getSendPort(),emailConfigPo.getEmailAddress(),emailConfigPo.getPassword());
        }
		Properties props = new Properties();
		props.setProperty("mail.smtp.connectiontimeout", "10000");
		mailer.applyProperties(props);
		
       /* try{
			mailer.sendMail(email);
        }catch(MailException e){
        	throw new AppException("邮箱验证失败，请查证后重试!");
        }*/
		mailer.sendMail(email);
		return 1;
	}
	
	@Override
	public Integer deleteEmailConfigInfo(String emailAccount) {
		emailConfigDao.deleteEmailInfo(emailAccount);
		return null;
	}

	@Override
	public EmailConfigPo queryEmailCfgByEmailAddress(String emailAddress) {
		return emailConfigDao.queryEmailCfgByEmailAddress(emailAddress);
	}

	@Override
	public IPage<EmailConfigPo> queryAllAccountsByPage(TaParamDto dto,int start, int limit,String grid) {
		return emailConfigDao.queryAllAccountsByPage(dto,start,limit,grid);
	}

	@Override
	public Integer updateEmailConfig(EmailConfigPo emailConfigPo) {
		try{
			//如果要修改修改为默认邮箱，把原来的默认邮箱清除
			if(emailConfigPo.getDefaultemail()==1){
				emailConfigDao.removeDefault();
			}
			emailConfigDao.updateEmailConfig(emailConfigPo);
			return 1;
		}catch(Exception e){
			return -1;
		}
		
	}

	@Override
	public List<SysAppVo> queryAllSystem() {
		return sysAppBpo.getSysAppList();
	}

	@Override
	public SysAppVo querySysById(String id) {
		return sysAppBpo.getSysAppById(id);
	}

	@Override
	public EmailConfigVo queryDefaultEmail() {
		return emailConfigDao.queryDefaultEmail();
	}

	@Override
	public EmailConfigVo queryEmailConfigBySystemId(String systemId) {
		if(systemId==null){
			return null;
		}
		List<EmailConfigVo> ecvs = emailConfigDao.queryAllEmailConfig();
		for (EmailConfigVo ecv : ecvs) {
			String sysIds = ecv.getSystemId();
			if(StringUtil.isNotEmpty(sysIds)){
				String[] ids = sysIds.trim().split(",");
				for (int i = 0; i < ids.length; i++) {
					if(systemId.equals(ids[i])){
						return ecv;
					}
				}
			} 
		}
		return null;
	}

	@Override
	public Integer updateEmailConfigWithValidate(EmailConfigPo emailConfigPo) {
		//验证邮箱
		validateEmailAddress(emailConfigPo);
		try{
			//如果要修改修改为默认邮箱，把原来的默认邮箱清除
			if(emailConfigPo.getDefaultemail()==1){
				emailConfigDao.removeDefault();
			}
			emailConfigDao.updateEmailConfig(emailConfigPo);
			return 1;
		}catch(Exception e){
			return -1;
		}
	}
}
